package cn.edu.nju.ws.sview.servlets;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashSet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.nju.ws.sview.LogManagement;
import cn.edu.nju.ws.sview.reasoning.provenance.FromRDFDocument;
import cn.edu.nju.ws.sview.util.URIUtil;
import cn.edu.nju.ws.sview.views.User;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String userName = request.getParameter("uid");
		String displayLang = request.getParameter("display-lang") ;
		request.getSession().setAttribute("user", userName) ;
		request.getSession().setAttribute("firstLogin", true) ;
		request.getSession().setAttribute("display-lang", displayLang) ;
		int userId = User.getUserIdByName(userName);
		LogManagement.storeLogInOut(userId, LogManagement.EVENT_LOGIN, displayLang);
		String from = request.getParameter("from");
		if(from == null){
			String location = "/views/index.jsp";
			User user = new User(userName) ;
			HashSet<String> langs = user.getPreferedLanguages();
			if(langs.size()==1&&langs.contains("zh")){
				location = location+"?lang=zh" ;
			}else
				location  = location +"?lang="+displayLang;
			
			request.getRequestDispatcher(location).forward(request, response) ;
		}else if(from.equals("relations")){
			String langString ="en";
			User user = new User(userName) ;
			HashSet<String> langs = user.getPreferedLanguages();
			if(langs.size()==1&&langs.contains("zh")){
				langString = "zh";
			}
			
			response.sendRedirect("./relations/index.jsp?lang=" + langString);
		}else{
			from = URLDecoder.decode(from, "utf-8");
			String lang = URIUtil.testParam(from, "lang") ;
			User user = new User(userName) ;
			HashSet<String> langs = user.getPreferedLanguages();
			if(!langs.contains(lang)){
				from = URIUtil.removeParam(from, "lang") ;
				String langString ="en";
				if(langs.size()==1&&langs.contains("zh")){
					langString = "zh";
				}
				
				if(from.contains("?")){
					from = from+"&lang="+langString;
				}else{
					from = from+"?lang="+langString;
				}
			}
//			System.out.println("from:"+from);
			response.sendRedirect(from);
		}
	}
}
